package com.wws.family.ui.activity

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.wika.basics.base.BaseActivity
import com.wika.basics.ext.divider
import com.wika.basics.ext.toColorSpan
import com.wika.basics.ext.vertical
import com.wws.family.R
import com.wws.family.adapter.NoteBookAdapter
import com.wws.family.databinding.ActivityNoteBookBinding
import com.wws.family.db.NoteBookBean
import com.wws.family.db.WiKaDataBase
import com.wws.family.dialog.NoteBookInputDialog
import com.wws.family.dialog.CommonDialog
import com.wws.family.helper.OnStartDragListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
 *@Description:记事本页面
 *@Date: 2025/1/8 15:35
 *@Author: WangWeiShuo
 */
class NoteBookActivity: BaseActivity<ActivityNoteBookBinding>(),OnStartDragListener {
    private var noteBookAdapter: NoteBookAdapter? = null
    private var noteBookList = mutableListOf<NoteBookBean>()
    private var itemTouchHelper: ItemTouchHelper? = null
    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(R.drawable.baseline_arrow_back,"记事本",R.color.black,R.color.blue_B8DCFF,R.drawable.icon_add,true,true)
    }

    override fun initData() {
        initNoteBookInfo()
    }

    override fun needFullScreen(): Boolean {
        return false
    }

    override fun actionBarRightClick() {
        NoteBookInputDialog.Builder(this).addSaveListener(object : NoteBookInputDialog.OnSaveListener{
            override fun onSave(name: String, time: String,remark: String) {
                lifecycleScope.launch(Dispatchers.IO){
                    val notebookBean = NoteBookBean(name,time,remark)
                    WiKaDataBase.getInstance().notebookDao().insert(notebookBean)
                    noteBookList.add(0,notebookBean)
                    checkData()
                    delay(500)
                    withContext(Dispatchers.Main){
                        if(noteBookAdapter != null){
                            noteBookAdapter?.setItem(0,notebookBean)
                        }
                    }
                }
            }

        }).show()
    }

    private fun initNoteBookInfo(){
        lifecycleScope.launch(Dispatchers.IO){
            noteBookList = WiKaDataBase.getInstance().notebookDao().getNoteBookInfo()
            withContext(Dispatchers.Main){
                showCommemorationInfo()
            }
        }
    }

    private fun checkData(){
        if(noteBookList.isNullOrEmpty()){
            mBinding.ivNoData.visibility = View.VISIBLE
            mBinding.rvNoteBook.visibility = View.INVISIBLE
        }else{
            mBinding.ivNoData.visibility = View.INVISIBLE
            mBinding.rvNoteBook.visibility = View.VISIBLE
        }
    }

    private fun showCommemorationInfo(){
        checkData()
        if(noteBookAdapter == null){
            noteBookAdapter = NoteBookAdapter(this)
            mBinding.rvNoteBook.vertical()
//            mBinding.rvNoteBook.divider(color = getColor(R.color.gray_f0f0f0), paddingSize = 60, includeLast = false)
            mBinding.rvNoteBook.adapter = noteBookAdapter
        }
        noteBookAdapter?.setData(noteBookList)
        noteBookAdapter?.addDeleteCallBack(object : NoteBookAdapter.OnDeleteCallBack {
            override fun onDelete(pos: Int, itemBean: NoteBookBean?) {
                showDeleteDialog(pos, itemBean)
            }
        })
    }

    override fun onStartDrag(viewHolder: RecyclerView.ViewHolder?) {
        itemTouchHelper?.startDrag(viewHolder!!)
    }

    private fun showDeleteDialog(pos: Int, itemBean: NoteBookBean?){
        val deleteValue = "是否删除${itemBean?.name}?"
        val deleteContent = deleteValue.toColorSpan(IntRange(4,deleteValue.length - 1),getColor(R.color.red_DD2C00))
        CommonDialog.Builder(this).showTitle("删除提示").showContent(deleteContent)
            .setOnSureListener {
                noteBookList.removeAt(pos)
                if(noteBookAdapter != null){
                    noteBookAdapter?.removeAt(pos)
                }
                checkData()
                lifecycleScope.launch(Dispatchers.IO){
                    WiKaDataBase.getInstance().notebookDao().deleteByName(itemBean?.name!!)
                }
            }.show()
    }
}